<template>
    <div>

        <!-- 表单 -->
        <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
			<span>
				<el-form ref="form" :rules="rules" :model="form" label-width="80px">
					<el-form-item label="厂家名称" prop="producerName">
						<el-input v-model="form.producerName"></el-input>
					</el-form-item>
					<el-form-item label="厂家编码" >
						<el-input v-model="form.producerCode"></el-input>
					</el-form-item>
					<el-form-item label="联系人" prop="producerPerson">
						<el-input v-model="form.producerPerson"></el-input>
					</el-form-item>
					<el-form-item label="电话" prop="producerTel">
						<el-input v-model="form.producerTel"></el-input>
					</el-form-item>
					<el-form-item label="关键字">
						<el-input v-model="form.keywords"></el-input>
					</el-form-item>
					<el-form-item label="状态">
						<el-radio v-model="form.status" label="0">正常</el-radio>
						<el-radio v-model="form.status" label="1">禁用</el-radio>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="onSubmit('form')">立即创建</el-button>
						<el-button @click="aaa()">取消</el-button>
					</el-form-item>
				</el-form>
			</span>
        </el-dialog>


        <div>
            厂家名称<el-input v-model="searchForm.producerName" placeholder="请输入厂家名称" style="width: 100px;"></el-input>
            关键字<el-input v-model="searchForm.keywords" placeholder="请输入关键字" style="width: 100px;"></el-input>
            状态<el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option
                    v-for="item in statusOptions"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue">
            </el-option>
        </el-select>
            <el-button type="primary" @click="queryAll()">搜索</el-button>
            <el-button type="primary" @click="resetFormFiled()">重置</el-button>
        </div>
        <div>
            <el-button type="primary" style="float: left;" @click="addUser()">添加</el-button>
            <el-button type="danger" @click="addUser1()" round>删除</el-button>
        </div>
        <el-table
                ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column fixed prop="producerId" label="厂家id" width="120">
            </el-table-column>
            <el-table-column prop="producerName" label="厂家名称" width="300">
            </el-table-column>
            <el-table-column prop="producerCode" label="厂家编码" width="120">
            </el-table-column>
            <el-table-column prop="producerPerson" label="联系人" width="120">
            </el-table-column>
            <el-table-column prop="producerTel" label="电话" width="150">
            </el-table-column>
            <el-table-column prop="keywords" label="关键字" width="120">
            </el-table-column>
            <el-table-column prop="dictDatal.dictLabel" label="状态" width="120">
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间" width="200">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="100">
                <template slot-scope="scope">
                    <el-button @click="updUser(scope.row)" type="text" size="small" icon="el-icon-edit">修改</el-button>
                    <el-button @click="delUser(scope.row)" type="text" size="small" icon="el-icon-delete">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page.current"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="page.seze"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "Producter",
        methods:{
            //状态字典解析
            statusformat(row){
                let val=row.status ;
                return this.formatDict(this.statusOptions,val)
            },

            //状态
            initStatus(){
                this.$axios.get("system/api/dict/data/findByType/sys_normal_disable").then(r=>{
                    this.statusOptions = r.data.t;
                })
            },
            //批量
            handleSelectionChange(val) {
                this.multipleSelection = val;

            },
            queryAll(){
                this.page.current=1;
                this.initTable();
            },
            //状态下拉框
            queryDept(){
                this.$axios.get('medicine/producer/getAll').then(r=>{
                    this.deptData=r.data.t.t;
                })
            },
            resetFormFiled(){
                this.searchForm={};
                this.initTable();
            },
            //分页
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.page.size=val;
                this.initTable();
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.page.current=val;
                this.initTable();
            },
            //修改
            updUser(row){
                this.dialogVisible=true;
                this.form=JSON.parse(JSON.stringify(row));
            },
            //提交表单
            onSubmit(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post('medicine/producer/addProducerMapper',this.form).then(r=>{
                            this.dialogVisible=false;
                            this.form={};
                            this.initTable();
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },
            aaa(){
                this.dialogVisible=false;
                this.form={};
            },
            // 添加
            addUser() {
                this.form={status:"0"};
                this.dialogVisible=true;

            },
            // 删除
            delUser(row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.delete(`medicine/producer/delById/${row.producerId}`).then(r => {
                        alert(r.data.msg);
                        this.initTable();
                    })
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            //批量删除
            addUser1(){
                // let aaa = []
                // let bbb=[]
                // this.multipleSelection.forEach(item=>{
                //     aaa = {providerId:item.providerId}
                //     bbb.push(aaa)
                // })
                // console.log(JSON.stringify(bbb))
                // alert(JSON.stringify(this.multipleSelection))
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.post("medicine/producer/delByIds", this.multipleSelection).then(r => {
                        this.initTable();
                    })
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },

            //刷新表格  //模糊查询

            initTable() {
                let search={...this.page,...this.searchForm};
                this.$axios.get('medicine/producer/getAll/',{params:search}).then(r => {
                    this.tableData = r.data.t.records;
                    //刷新总条数
                    this.page.total=r.data.t.total;
                })
            }
        },
        data() {
            return {
                tableData: [],
                dialogVisible: false,
                form: {},
                page:{
                    total:10,
                    current:1,
                    size:10
                },
                form:{
                    producerName:'',
                    producerPerson:'',
                    producerTel:'',
                },
                searchForm: {},
                deptData:[],
                multipleSelection: [],
                statusOptions:[],
                rules: {
                    producerName: [
                        { required: true, message: '请输入厂家名称3-10', trigger: 'blur' },
                        { min: 3, max: 20, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    producerPerson: [
                        { required: true, message: '联系人不能为空', trigger: 'blur' }
                    ],
                    producerTel: [
                        { required: true, message: '请输入正确的手机号', trigger: 'blur' },
                        { min: 1, max: 21, message: '长度在 1 到 11 个字符', trigger: 'blur' }
                    ],
                }
            }
        },
        created() {
            this.initTable();
            this.queryDept();
            this.initStatus();
        }
    }
</script>

<style scoped>

</style>
